import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.BiConsumer;

public class Main {
    public static void main(String[] args) {
        orderMap1();
    }


    // 遍历Map的四种方式
    // 先获取Map集合的全部key的set集合，遍历map的key的Set集合，通过map的key提取对应的value。
    public static void orderMap1(){
        Map<Integer,String> map1 = new TreeMap<>();
        map1.put(1,"张三");
        map1.put(3,"lisi");
        map1.put(5,"lisi");
        Set<Integer> mapKey = map1.keySet();
        for (Integer key : mapKey) {
            String value = map1.get(key);
            System.out.print(key + " " + value + "   ");
        }
    }
    // 使用foreach遍历把map的key和value打包成Set的key后的这个Set集合
    public static void orderMap2(){
        Map<Integer,String> map1 = new TreeMap<>();
        map1.put(1,"张三");
        map1.put(3,"lisi");
        map1.put(5,"lisi");
        Set<Map.Entry<Integer, String>> entries = map1.entrySet();
        for (Map.Entry<Integer,String> entry : entries) {
            System.out.print(entry.getKey() + " " + entry.getValue() + "   ");
        }
    }
    // new一个BiConsumer<key, value>()
    public static void orderMap3(){
        Map<Integer,String> map1 = new TreeMap<>();
        map1.put(1,"张三");
        map1.put(3,"lisi");
        map1.put(5,"lisi");

        map1.forEach(new BiConsumer<Integer, String>() {
            @Override
            public void accept(Integer integer, String s) {
                System.out.print(integer + " " + s + "   ");
            }
        });
    }

    public static void orderMap4() {
        Map<Integer, String> map1 = new TreeMap<>();
        map1.put(1, "张三");
        map1.put(3, "lisi");
        map1.put(5, "lisi");

        map1.forEach((k,v)->{
            System.out.print(k + " " + v + "   ");
        });
    }
}
